package server.model.administration;

import java.rmi.RemoteException;
import java.rmi.server.UnicastRemoteObject;
import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;

import server.model.meeting.Announcement;
import server.model.meeting.Group;
import server.model.meeting.Meeting;
import server.model.meeting.MeetingInfo;
import server.model.meeting.Participant;
import server.model.meeting.User;
import server.model.meeting.UserInfo;
import server.model.remotes.IMeeting;
import server.model.remotes.IMeetingService;

public class MeetingService extends UnicastRemoteObject implements IMeetingService {

	private ArrayList<Meeting> meetings;
	private Iterator<Meeting> meetingIterator;
	private Admin admin;

	private int meetingCount;
	private ArrayList<Announcement> announcements;
	private ArrayList<Participant> participants;
	
	
	public MeetingService(Admin admin)throws RemoteException{
		super();
		
		this.admin = admin;
		
		meetingCount = 0;
		meetings = new ArrayList<Meeting>(10);
		meetingIterator = meetings.iterator();
		announcements = new ArrayList<Announcement>(10);	
		participants = new ArrayList<Participant>();
	}
			
	public MeetingInfo[] getMeetingsInfo() throws RemoteException{

		MeetingInfo[] meetingInfos = new MeetingInfo[meetingCount];
		int i = 0;
		meetingIterator = meetings.iterator();
		while(meetingIterator.hasNext()){
			Meeting currentMeeting = meetingIterator.next();
			meetingInfos[i] = currentMeeting.getMeetingInfo();
			i++;
		}
		return meetingInfos;
	}

	public IMeeting attendMeeting(String userName, String meetingName) throws RemoteException {
		meetingIterator = meetings.iterator();
		while(meetingIterator.hasNext()){
			Meeting currentMeeting = meetingIterator.next();
			if (meetingName.matches(currentMeeting.getMeetingInfo().getName())){
				if(isInvited(userName, currentMeeting)){
					return currentMeeting;
				}
			}
		}
		return null;		
	}
	public boolean isInvited(String userName, Meeting meeting)
	{
		ArrayList<Participant> currentParticipants = meeting.getParticipants();
		for(int i = 0; i < currentParticipants.size(); i++){
			if(currentParticipants.get(i).getClass().toString().equalsIgnoreCase("class server.model.meeting.Group")){
				if(isInGroup(userName, (Group)currentParticipants.get(i)))return true;
			}
			if(currentParticipants.get(i).getName().equalsIgnoreCase(userName)){
				return true;
			}
		}
		return false;
	}
	public boolean isInGroup(String userName, Group group)
	{
		ArrayList<Participant> currentParticipants = group.getParticipants();
		for(int i = 0; i < currentParticipants.size(); i++){
			if(currentParticipants.get(i).getClass().toString().equalsIgnoreCase("class server.model.meeting.Group")){
				if(isInGroup(userName, (Group)currentParticipants.get(i))) return true;
			}
			if(currentParticipants.get(i).getName().equalsIgnoreCase(userName)){
				return true;
			}
		}
		return false;
	}
	public boolean addMeeting(String name, boolean openMeeting,	String chairman, Date start, Date end, String[] participants)throws RemoteException {
		if(!meetingExists(name)){
			Meeting meeting = new Meeting();
			MeetingInfo meetingInfo = new MeetingInfo();
			meetingInfo.setName(name);
			meetingInfo.setStartDate(start);
			meetingInfo.setEndDate(end);
			meetingInfo.setOpenMeeting(openMeeting);
			meetingInfo.setChairman((User)admin.getParticipantByName(chairman));
				
			for (int i=0; i<participants.length; i++){		
				meeting.addParticipant(admin.getParticipantByName(participants[i]));
			}
			meeting.setMeetingInfo(meetingInfo);
			meetings.add(meeting);
			meetingCount++;
			return true;	
		}
		return false;
	}
	public boolean meetingExists(String meetingName)
	{
		meetingIterator = meetings.iterator();
		while(meetingIterator.hasNext()){
			Meeting currentMeeting = meetingIterator.next();
			if (meetingName.matches(currentMeeting.getMeetingInfo().getName())){
				return true;
			}
		}
		return false;
	}

	@Override
	public ArrayList<Announcement> getAnnouncements() throws RemoteException {
		return announcements;
	}

	@Override
	public void addAnnouncement(Announcement announcement)throws RemoteException {
		announcements.add(announcement);
	}

	@Override
	public ArrayList<Participant> addParticipants(Participant participant)
			throws RemoteException {
		participants.add(participant);
		
		return participants;
	}
}
